@extends('admin.layout.layout', ['row' => @$page])

@section('contain')
    <style>
        .table .tth{
            background-color: rgba(0, 0, 0, 0.05);
            width: 150px;
            font-weight: bold;
        }
        .table .ttd{
            width: 350px;
        }
        .table .ttt{
            padding: 0;
        }
        .ttt .table{
            margin: 0;
        }
        .ttt .table img + p{
            text-align: center;
            margin: 10px 0 0;
        }
        .tst{
            color: #00b050;
            font-weight: 700;
            text-align: center;
            font-size: 1.25rem;
        }
    </style>

    <div class="portlet">
        <div class="portlet-title">
            <div class="caption">
                <x-portlet-breadcrumb :page="$page"></x-portlet-breadcrumb>
            </div>
            <div class="actions"></div>
        </div>
        <form action="/{{ request()->path() }}/../" method="post" class="warranty-form t-right">
            @csrf
            <input type="text" name="_previous_" value="{{ request()->header('referer') }}" hidden>

            <table class="table s3">
                <tbody>
                @if(session('admin.user_type') == 2)
                <tr>
                    <td class="tth">{{ ts('AZFS') }}</td>
                    <td class="ttd">
                        <select name="install_type" id="" class="form-control" onchange="az_change(this.value)">
                            <option value="0">{{ ts('ptaz') }}</option>
                            @if(session('admin.user_type') == 2)
                                <option value="1">ASP</option>
                            @endif
                            {{--@if(session('admin.user_type') == 2)
                                @foreach(getDPDs(session('admin.id')) as $v)
                                    <option value="{{ @$v->id }}">{{ @$v->precut_id }}</option>
                                @endforeach
                            @endif--}}
                        </select>
                    </td>
                    <td colspan="4" class="text-danger">{{ ts('wcit3') }}</td>
                </tr>
                @endif
                <tr>
                    <td colspan="6" class="tst">{{ ts('CUSTOMERinformation') }}</td>
                </tr>
                <tr>
                    <td class="tth">{{ ts('CUSTOMERNAME') }}</td>
                    <td class="ttd">
                        <input type="text" name="first_name" value="{{ $data->first_name ?: old('first_name') }}" class="form-control">
                    </td>
                    <td class="tth">{{ ts('phonenumber') }}</td>
                    <td class="ttd">
                        <div class=" input-group custom-control-inline">
                            <div class="input-group-prepend">
                                <select name="extension" class="form-control">
                                    @foreach(\App\Models\PhoneExtension::get(['id', 'phone_ext']) as $v)
                                        <option value="{{ $v->phone_ext }}" {{ ($data->extension == $v->phone_ext || old('phone_number')==$v->phone_ext ) ? 'selected' : '' }}>{{ $v->phone_ext }}</option>
                                    @endforeach
                                </select>
                            </div>
                            <input type="number" name="phone_number" value="{{ $data->phone_number ?: old('phone_number') }}" class="form-control">
                        </div>
                    </td>
                    <td class="tth">{{ ts('email') }}</td>
                    <td class="ttd">
                        <input type="text" name="email_address" value="{{ $data->email_address ?: old('email_address') }}" class="form-control">
                    </td>
                </tr>
                <tr>
                    <td class="tth">{{ ts('region') }}</td>
                    <td>
                        <select name="region_id" id="" class="form-control region_json" data-value="{{ $data->region_id ?: old('region_id') }}">
                            <option value="">{{ ts('select') }}</option>
                            <option value="{{ $data->region_id }}" selected>{{ ts('region_master.'.$data->region_id) }}</option>
                        </select>
                    </td>
                    <td class="tth">{{ ts('province') }}</td>
                    <td>
                        <select name="province_id" id="" class="form-control province_json" style="width: 100%;" data-value="{{ $data->province_id ?: old('province_id') }}">
                            <option value="">{{ ts('select') }}</option>
                            <option value="{{ $data->province_id }}" selected>{{ ts('province_master.'.$data->province_id) }}</option>
                        </select>
                    </td>
                    <td class="tth">{{ ts('city') }}</td>
                    <td>
                        <select name="city" id="" class="form-control city_json" style="width: 100%;" data-value="{{ $data->city ?: old('city') }}">
                            <option value="">{{ ts('select') }}</option>
                            <option value="{{ $data->city }}" selected>{{ ts('cities.'.$data->city) }}</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td class="tth">{{ ts('czdz') }}</td>
                    <td colspan="3">
                        <input type="text" name="address" value="{{ $data->address ?: old('address') }}" class="form-control">
                    </td>
                    <td class="tth"></td>
                    <td></td>
                    {{--<td class="tth">{{ ts('zip') }}</td>
                    <td>
                        <input type="text" name="zip" value="{{ $data->zip ?: old('zip') }}" class="form-control">
                    </td>--}}
                </tr>
                <tr>
                    <td colspan="6" class="tst">{{ ts('VEHICLEINFORMATION') }}</td>
                </tr>
                <tr>
                    <td class="tth">{{ ts('clnf') }}</td>
                    <td>
                        <select name="year_id" id="" class="form-control make_json_year" data-value="{{ $data->year_id ?: old('year_id') }}">
                            <option value="">{{ ts('select') }}</option>
                            <option value="{{ $data->year_id }}" selected>{{ $data->year_id }}</option>
                        </select>
                    </td>
                    <td class="tth">{{ ts('make') }}</td>
                    <td>
                        <select name="make_id" id="make_id" class="form-control" style="width: 100%" data-value="{{ $data->make_id ?: old('make_id') }}">
                            <option value="">{{ ts('select') }}</option>
                            @if (!empty($makes))
                                @foreach($makes as $item)
                                <option value="{{  $item->simplified_chiness_value }}">{{ $locale == 'en' ? $item->english_value : ($locale == 'zh_tw' ? $item->traditional_chiness_value : $item->simplified_chiness_value) }}</option>
                                @endforeach
                            @endif
                        </select>
                    </td>
                    <td class="tth">{{ ts('model') }}</td>
                    <td>
                        <select name="model_id" id="model_id" class="form-control make_model" data-value="{{ $data->model_id ?: old('model_id') }}">
                            <option value="">{{ ts('select') }}</option>
                        </select>
                        {{--<input type="text" name="model_id" value="{{ $data->model_id ?: old('model_id') }}" class="form-control" >--}}
                    </td>
                </tr>
                <tr>
                    <td class="tth">{{ ts('licenseplate') }}</td>
                    <td>
                        <input type="text" name="license_plate" value="{{ $data->license_plate ?: old('license_plate') }}" class="form-control" >
                    </td>
                    <td class="tth">{{ ts('vinnumber') }}</td>
                    <td colspan="3">
                        <input type="text" name="vin_number" value="{{ $data->vin_number ?: old('vin_number') }}" class="form-control" >
                    </td>
                <tr>
                    <td colspan="6" class="tst">{{ ts('INSTALLATIONINFORMATION') }}</td>
                </tr>
                <tr>
                    @if(session('admin.user_type')==1)
                        <td class="tth">{{ ts('distributor') }}</td>
                        <td>
                            <select class="form-control roll_user_id" name="sdid" style="width: 100%;">
                                @foreach(getDistributors() as $v)
                                    <option value="{{ $v->id }}">{{ $v->abbr ?: $v->company_name }}</option>
                                @endforeach
                            </select>
                        </td>
                        <td class="tth">{{ ts('ZTMD') }}</td>
                        <td>
                            <select class="form-control roll_user_id" style="width: 100%" name="user_id"></select>
                        </td>
                    @elseif (session('admin.user_type') == 4)
                        <td class="tth">{{ ts('distributor') }}</td>
                        <td>
                            <select class="form-control roll_user_id" name="sdid" style="width: 100%;">
                                @foreach(getDistributions(session('admin.id')) as $v)
                                    @if (session('admin.id') != $v->id)
                                    <option value="{{ $v->id }}">{{ $v->abbr ?: $v->company_name }}</option>
                                    @endif
                                @endforeach
                            </select>
                        </td>
                        <td class="tth">{{ ts('ZTMD') }}</td>
                        <td>
                            <select class="form-control roll_user_id" style="width: 100%" name="user_id"></select>
                        </td>
                    @else
                        <td class="tth">{{ ts('distributor') }}</td>
                        <td>
                            <input type="text" class="form-control sdjc" disabled name="sdjc" value="{{ getJc(session('admin.id')) }}">
                        </td>
                        <td class="tth">{{ ts('qudao') }}</td>
                        <td>
                            <select class="form-control roll_user_id" name="user_id">
                                @foreach(getDDs(session('admin.id'),session('admin.warranty_self')) as $v)
                                    <option value="{{ $v->id }}" {{ ($data->user_id==$v->id || old('user_id')==$v->id) ? 'selected' : ''  }}>{{ $v->abbr ?: $v->company_name }}</option>
                                @endforeach
                            </select>
                        </td>
                    @endif
                    <td colspan="2" class="text-danger">
                        @empty($data->pre_id)
                            {{ ts('wcit') }}
                        @else
                            {{ ts('wcit2') }}
                        @endif
                    </td>
                </tr>
                <tr>
                    <td class="tth">{{ ts('INSTALLERNAME') }}</td>
                    <td>
                        <input type="text" name="installer_name" value="{{ $data->installer_name ?: old('installer_name') }}" class="form-control" >
                    </td>
                    <td class="tth">{{ ts('INSTALLATIONDATE') }}</td>
                    <td>
                        <div class="input-group">
                            <input type="text" name="installation_date" value="{{ $data->installation_date ?: old('installation_date') }}" class="form-control flatpickr-ymdhis" data-max="{{ date('Y-m-d H:i:s') }}">
                            <div class="input-group-prepend">
                                <span class="btn btn-primary fkr-clear"><i class="fa fa-times"></i></span>
                            </div>
                        </div>
                    </td>
                    <td class="tth">{{ ts('INSTALLERPRICE') }}</td>
                    <td>
                        <input type="text" name="installation_price" value="{{ $data->installation_price ?: old('installation_price') }}" class="form-control" >
                    </td>
                </tr>
                <tr>
                    <td colspan="6" class="tst">{{ ts('INSTALLATIONPICTURE') }}</td>
                </tr>
                <tr>
                    <td colspan="6" class="ttt p-3">
                        {{--@include('admin.components.aztp-tb')--}}
                        <x-warranty-image-upload :warranty="$data"></x-warranty-image-upload>
                    </td>
                </tr>


                <tr>
                    <td colspan="6" class="ttt">
                        <x-warranty-image-demo></x-warranty-image-demo>
                    </td>
                </tr>

                <tr>
                    <td colspan="6" class="tst">{{ ts('VEHICLECOVERAGE') }}</td>
                </tr>
                <tr>
                    <td colspan="6" class="ttt p-0">
                        @include('admin.subview.coverage', ['coverages' => @getWVCByID(@$data->id), 'is_precut'=>@$data->pre_id ? 1 : 0])
                        {{--<div class="container-fluid">
                            @include('admin.subview.warranty_coverage', ['coverages' => $data->warranty_to_vehicle_coverage])
                        </div>--}}
                    </td>
                </tr>
                <tr>
                    <td colspan="6" class="tst">{{ ts('BCSM') }}</td>
                </tr>
                <tr>
                    <td colspan="6" height="auto">
                        <textarea name="additional_comments"  height="auto" rows="6" id="" class="form-control">{{ @$data->additional_comments ?: old('additional_comments') }}</textarea>
                    </td>
                </tr>
                </tbody>
            </table>

            <div class="mt-4">
                <button type="button" class="btn btn-info" onclick="saveWarranty(this)">{{ ts('store') }}</button>
                <button type="submit" class="btn btn-primary ml-5">{{ ts('submit') }}</button>
            </div>
        </form>
    </div>


@endsection

@section('script')
    <script>
        $(function () {
           $("#make_id").on("change", function () {
               console.log($(this).val());
               request.get('/admin/setting-model/getModels', {
                   params: {
                       make_name: $(this).val(),
                   },
                   timeout: 30000,
               })
               .then( function(d) {
                   getModels(d);
                })
               .catch(function (d) {
                   getModels(d);
               })
           });
        });
        function getModels(d) {
            if (d != '' || Array.isArray(d) && d.length >= 1) {
                let selectHtml = '<option value="">请选择</option>';
                for (let i in d) {
                    if (typeof d[i] === 'undefined' || typeof d[i]['simplified_chiness_value'] === 'undefined') {
                        continue;
                    }
                    @if ($locale == 'en')
                    selectHtml += '<option value="' + d[i]['english_value'] + '">' + d[i]['english_value'] + '</option>';
                    @elseif ($locale == 'zh_tw')
                        selectHtml += '<option value="' + d[i]['traditional_chiness_value'] + '">' + d[i]['traditional_chiness_value'] + '</option>';
                    @else
                        selectHtml += '<option value="' + d[i]['simplified_chiness_value'] + '">' + d[i]['simplified_chiness_value'] + '</option>';
                    @endif
                }
                $("#model_id").empty().append(selectHtml);
            } else {
                $("#model_id").empty();
            }
        }

        function saveWarranty(dom) {
            $(dom).attr('disabled', true);
            var form = formObjV2('.warranty-form');
            var state = false;
            var fields = ['first_name','phone_number','email_address','region_id','province_id','city','address','year_id','make_id','model_id','license_plate','vin_number','installer_name','installation_date','installation_price'];
            for (var i = 0; i < fields.length; i++) {
                if (form[fields[i]]) {
                    state = true;
                    console.log(fields[i], form[fields[i]])
                }
            }
            if (!state) {
                alert('车主信息、车辆信息、安装信息不能全部为空！');
                $(dom).attr('disabled', false);
                return false;
            }
            form.t = 'save';
            var url = '/admin/ajax/warranty';

            request.post(url, form)
                .then(function (d) {
                    var data = d.data;
                    swal('', d.msg || '保存成功！', 'success')
                        .then(function (d) {
                            var url = '/admin/warranty';
                            if (data && data.id) {
                                url = '/admin/warranty/'+data.id+'/edit';
                            }
                            window.location.assign(url)
                            // window.location.assign(window.location.href)
                            // window.location.assign('./../')
                        })
                })
                .catch(function (d) {
                    alert(d.msg || '保存失败');
                    $(dom).attr('disabled', false);
                })
        }


        $('body').on('change', '[name="sdid"]', function () {
            var val=$(this).val();
            var $that = $(this)
            request.get('/admin/ajax/getUsers', {
                params: {
                    t: 'd',
                    user_id: val
                }
            })
                .then( function(d) {
                    var data = d.data
                    $('.portlet select[name="user_id"]').empty().append(makeSelectOptionsOnly(data))
                })
                .catch(function (d) {
                    toastr.error(d.msg)
                })
        });

        $(document).ready(function () {
            $("#make_id,.province_json,.city_json,.warranty_part").select2();
            $(".roll_user_id").select2();
            /*
            * 根据手机号码更新*/
            $('input[name="phone_number"]').on('change', function () {
                var val = $(this).val();
                var form = {
                    t: 'getInfoByPhone',
                    phone_number: val,
                };
                if (val) {
                    request.post('/admin/ajax/warranty', form)
                        .then(function (d) {
                            var data = d.data;
                            console.log(data);
                            if (data && data.phone_number) {
                                var msg = '系统中有相同手机号的顾客信息，是否复用该信息作为顾客信息？';
                                var r = confirm(msg);
                                if (r == true) {
                                    $('input[name="first_name"]').val(data.first_name);
                                    $('input[name="email_address"]').val(data.email_address);
                                    $('select[name="region_id"]').val(data.region_id).trigger('change');
                                    $('select[name="province_id"]').val(data.province_id).trigger('change');
                                    $('select[name="city"]').val(data.city);
                                    $('input[name="address"]').val(data.address);
                                    $('input[name="zip"]').val(data.zip);
                                }
                            }
                        })
                }
            });


            if ($('[name="pre_id"]').val()) {
                getPrecutDetail();
            } else {
                if ($('[name="sdid"]').length >=1 ) {
                    request.get('/admin/ajax/getUsers', {
                        params: {
                            t: 'd',
                            user_id: $('[name="sdid"]').val()
                        }
                    })
                        .then( function(d) {
                            var data = d.data
                            $('.portlet select[name="user_id"]').empty().append(makeSelectOptionsOnly(data))
                            initWVCRows();
                        })
                        .catch(function (d) {
                            toastr.error(d.msg)
                        })
                } else {
                    initWVCRows();
                }
                // initCoverageRowsV2();

            }


        })
        $('.roll_user_id[name="user_id"]').on('change', function () {
            initCoverageRowsV2([]);
        })
        function getPrecutDetail() {
            var id = $('[name="pre_id"]').val()
            request.get('/admin/ajax/getPVC', {
                params: {
                    id: id,
                }
            }).then(function (d) {
                var obj = d.data;
                var jxs = d.jxs;
                initCoverageRowsV2(obj);
                $('[name="user_id"]').val(jxs.user_id)
                $('input[name="sdjc"]').val(jxs.sdmc)
                $('select[name="user_id"]').empty().append(makeSelectOptionsOnly([{label: jxs.jxsmc, value: jxs.user_id}]))
            })
                .catch(function (d) {

                })
        }
    </script>
@endsection